export default {
  name: 'goodsList',
  components: {},
  data() {
    return {
      list: [],
      fields: [{
        width: '100',
        key: 'name',
        label: '书名'
      }, {
        width: '100',
        key: 'cover',
        label: '图片',
        type: 'image'
      }, {
        width: '250',
        key: 'describe',
        label: '简述',
        align: 'left'
      }, {
        width: '150',
        key: 'price',
        label: '价格',
      }, {
        width: '70',
        key: 'chapter_count',
        label: '章节数'
      }, {
        key: 'author',
        label: '作者'
      }, {
        key: 'word_count',
        label: '字数'
      }],

      //需要给分页组件传的信息
      paginations: {
        current_page: 1,
        total: 0,
        page_size: 9,
        page_sizes: [3, 9, 12, 24],
        layout: "total, sizes, prev, pager, next, jumper"
      },

      //搜索配置
      search_data: {
        fields: [{
          key: 'name',
          label: '书名',
          desc: '请输入书名'
        }],
        default_value: {
          name: ''
        }
      },

      //按钮配置
      btn_info: {
        batch: false,
        batch_delete: false,
        select: false,
        width: 300,
        add_text: '添加书籍',
        update_text: '修改',
        delete_text: '删除书籍',
        batch_delete_text: '批量删除书籍'
      }

    }
  },
  methods: {
		/**
		 * 获取文章列表
		 */
    onGetList() {
      this.$$api_book_bookList({
        data: {
          page: this.paginations,
          params: {
            name: this.search_data.default_value.name
          }
        },
        fn: data => {
          this.list = data;
        }
      });
    },


		/**
		 * 点击删除按钮
		 */
    onClickBtnDelete(opts) {
      this.$confirm('删除后不可恢复', '确认删除？').then(() => {
        new Promise((resolve, reject) => {
          this.$$api_goods_findSpecificationByGoodsID({
            data: opts.data.id,
            fn: resolve
          });
        }).then(next => {
          return new Promise((resolve, reject) => {
            this.$$api_goods_deleteSpecifications({
              data: next,
              fn: resolve
            });
          })
        }).then(next => {
          this.$$api_goods_findGoods({
            data: opts.data.id,
            fn: next => {
              this.$$api_goods_deleteGoods({
                data: next,
                fn: data => {
                  this.onGetList();
                }
              });
            }
          });
        });
      });
    },


		/**
		 * 添加商品
		 */
    onClickBtnAdd() {
      this.$router.push('/main/book/create');
    },


    onClickBtnSelect(opts) {
      this.$router.push({
        path: '/manage/goods/addressedit',
        query: {
          id: opts.data.id
        }
      });
    },


		/**
		 * 修改按钮
		 * @param opts
		 */
    onClickBtnUpdate(opts) {
      this.$router.push({
        path: '/main/book/edit',
        query: {
          id: opts.data.id
        }
      });
    },


		/**
		 * 改变页码事件
		 * @param current_page    当前页码
		 */
    onChangeCurPage(current_page) {
      var path = this.$route.path,
        query = Object.assign({}, this.$route.query);
      query.current_page = current_page;
      this.$router.push({
        path,
        query
      });
    },


		/**
		 * 改变每页显示数量事件
		 * @param page_size    每页显示的数量
		 */
    onChangePageSize(page_size) {
      var path = this.$route.path,
        query = Object.assign({}, this.$route.query);
      query.page_size = page_size;
      this.$router.push({
        path,
        query
      });
    },


		/**
		 * 更新参数
		 */
    onUpdateParams() {
      if (this.$route.query.current_page) {
        this.paginations.current_page = parseInt(this.$route.query.current_page);
      }
      if (this.$route.query.page_size) {
        this.paginations.page_size = parseInt(this.$route.query.page_size);
      }

      this.search_data.default_value.title = this.$route.query.title;
    },


		/**
		 * 搜索事件
		 * @param data    表单数据
		 * @param info    其他有用的数据
		 */
    onSearch({ data, info }) {
      console.log(data);
      console.log(info);

      var path = this.$route.path,
        query = Object.assign({}, this.$route.query, data);

      this.$router.push({
        path,
        query
      });
    },


    //批量选择改变CheckBox事件
    onSelectionChange({ ids, datas }) {
      // console.log(ids);
      // console.log(datas);
    },

		/**
		 * 批量删除
		 * @param ids 选中的ids
		 * @param datas	选中的数据集合
		 */
    onClickBtnBatchDelete({ ids, datas }) {
      this.$confirm('删除的数据：' + ids.join(','), '确认删除？').then(() => {
        this.$$api_goods_deleteGoods({
          data: {
            id: ids.join(',')
          },
          fn: data => {
            this.onGetList();
          }
        });
      });
    },


		/**
		 * 初始化
		 */
    init() {
      this.onUpdateParams();
      this.onGetList();
    }
  },
  mounted() {
    this.init();
  },
  watch: {
    $route(to, from) {
      this.init();
    }
  }

}